﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.DAL;

namespace WXYW
{
    public partial class BJXXDisagreeForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindForms();
            }
        }

        private void BindForms()
        {
            ApplicationProductService service = new ApplicationProductService();
            ApplicationProduct product = service.GetApplicationProductByID(long.Parse(Request.QueryString[Constants.QUERYSTR_PRODUCT_ID]));
            if (!product.ApplicationFormReference.IsLoaded)
            {
                product.ApplicationFormReference.Load();
            }
            if (!product.ApplicationForm.UserReference.IsLoaded)
            {
                product.ApplicationForm.UserReference.Load();
            }
            if (!product.ProductTypeReference.IsLoaded)
            {
                product.ProductTypeReference.Load();
            }

            contactLabel.Text = product.ApplicationForm.User.ContactPerson;
            companyLabel.Text = product.ApplicationForm.User.CompanyName;
            phoneNumberLabel.Text = product.ApplicationForm.User.UserPhone;
            cellPhoneLabel.Text = product.ApplicationForm.User.UserMobilePhone;
            faxLabel.Text = product.ApplicationForm.User.UserFax;
            addressLabel.Text = product.ApplicationForm.User.UserAddress;
            emailLabel.Text = product.ApplicationForm.User.UserEmail;
            agentLabel.Text = product.ApplicationForm.ProviderName;
            Label1.Text = product.ApplicationForm.ApplicationDateTime.Value.ToString("yyyy-MM-dd");
            Label2.Text = product.DealWithDateTime.HasValue ? product.DealWithDateTime.Value.ToString("yyyy-MM-dd") : "";

            //
            runningNumberLabel.Text = product.ApplicationForm.RunningNumber;
            clientNameLabel.Text = product.ApplicationForm.User.ContactPerson;
            productTypeLabel.Text = product.ProductType.ProductTypeName;
            productSerivalNumberLabel.Text = product.ProductSerialNumber;
            applyTimeLabel.Text = product.ApplicationForm.ApplicationDateTime.Value.ToString("yyyy-MM-dd");
            buyTimeLabel.Text = product.ProductPurchaseDateTime.Value.ToString("yyyy-MM-dd");
            statusLabel.Text = product.Status;
            reasonLabel.Text = product.ReasonOfNotRepair;
            repairTupeLabel.Text = product.ProductRepairType;
        }

        protected void reactiveButton_Click(object sender, EventArgs e)
        {
            Modalpopupextender1.Show();
        }

        protected void sendMailButton_Click(object sender, EventArgs e)
        {
            ApplicationProductService service = new ApplicationProductService();
            ApplicationProduct product = service.GetApplicationProductByID(long.Parse(Request.QueryString[Constants.QUERYSTR_PRODUCT_ID]));
            product.Status = "已提交-待审批";
            product.ReasonOfNotRepair = string.Empty;
            product.BJXXReactiveReason = reasonTextBox.Text;
            product.BJXXDealResult = null;
            service.UpdateProduct(product);
            Modalpopupextender1.Hide();
            ClientScript.RegisterStartupScript(this.GetType(), "tip", "alert('成功激活该申请');location.href='BJXXListDisagreeForms.aspx'", true);
        }

        protected void turnToRMAButton_Click(object sender, EventArgs e)
        {
            ApplicationProductService service = new ApplicationProductService();
            ApplicationProduct product = service.GetApplicationProductByID(long.Parse(Request.QueryString[Constants.QUERYSTR_PRODUCT_ID]));
            if (!product.ApplicationFormReference.IsLoaded)
            {
                product.ApplicationFormReference.Load();
            }
            if (!product.ApplicationForm.UserReference.IsLoaded)
            {
                product.ApplicationForm.UserReference.Load();
            }
            if (!product.ProductTypeReference.IsLoaded)
            {
                product.ProductTypeReference.Load();
            }

            product.Status = "已转RMA";
            service.UpdateProduct(product);

            ApplicationService appService = new ApplicationService();

            ApplicationForm newForm = new ApplicationForm();
            newForm.ApplicationDateTime = DateTime.Now;
            newForm.ApplicationType = "R";
            newForm.User = product.ApplicationForm.User;
            newForm.IsSubmit = true;
            newForm.Status = "等待RMA单号确认";
            newForm.RunningNumber = RunningNumberService.GenerateApplicationNumber();

            if (!product.ProductTypeReference.IsLoaded)
            {
                product.ProductTypeReference.Load();
            }
            if (!product.ProblemTypeReference.IsLoaded)
            {
                product.ProblemTypeReference.Load();
            }

            ApplicationProduct newproduct = new ApplicationProduct
            {
                ProductType = product.ProductType,
                ProductCount = 1,
                ProductSerialNumber = product.ProductSerialNumber,
                ProductVersionNumber = product.ProductVersionNumber,
                ProblemType = product.ProblemType,
                ProductProblemDescription = product.ProductProblemDescription,
                ProductAppearance = product.ProductAppearance,
                ProductPurchaseDateTime = product.ProductPurchaseDateTime,
                ProductRepairType = "保内"
            };

            newForm.ApplicationProduct.Add(newproduct);

            long applicationID = appService.AddApplication(newForm);

            appService.SubmitApplication(applicationID);

            Response.Write(string.Format("<script language='JavaScript'> alert( '备件已生成维修申请单，流水号为:" + newForm.RunningNumber + "');location.href= 'BJXXListDisagreeForms.aspx' </script>"));
        }

        protected void tfsqButton_Click(object sender, EventArgs e)
        {
            ApplicationProductService service = new ApplicationProductService();
            ApplicationProduct product = service.GetApplicationProductByID(long.Parse(Request.QueryString[Constants.QUERYSTR_PRODUCT_ID]));

            SpecialServiceService specialService = new SpecialServiceService();
            specialService.AddSpecialServiceApplication(product);

            Response.Write(string.Format("<script language='JavaScript'> alert( '特服申请已经提交');location.href= 'BJXXListDisagreeForms.aspx' </script>"));
        }
    }
}
